import pygame.font
from pygame.sprite import Group

from mapping import Spacecraft_Mapping
from mapping import Bullet_Mapping


class Scoreboard:
    """显示游戏信息的类"""

    def __init__(self, settings, screen, stats, bullets, play_button):
        """初始化显示得分涉及的属性"""
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.settings = settings
        self.stats = stats
        self.bullets = bullets
        self.play_button = play_button

        # 显示得分信息时使用的字体设置
        self.text_color = (30, 30, 30)
        self.font = pygame.font.SysFont(None, 48)

        # 显示胜利信息时用的字体设置
        self.win_font = pygame.font.SysFont(None, 96, True, True)

        # 显示失败信息时有的字体设置
        self.lost_font = pygame.font.SysFont(None, 96, True, True)
        self.lost_str_color = (180, 0, 0)

        # 准备函数
        self.prep_all()

    def prep_all(self):
        """存储所有准备函数"""
        # 准备包含最高得分和初始得分图像
        self.prep_score()
        self.prep_highest_score()
        self.prep_level()

        # 准备飞船和子弹贴图
        self.prep_spacecraft_mappings()
        self.prep_bullet_mapping()

        # 准备胜利和失败信息图像
        self.prep_win_str()
        self.prep_lost_str()

    def prep_score(self):
        """将得分转换为一幅渲染图像"""
        rounded_score = round(self.stats.score, -1)
        score_str = "{:,}".format(rounded_score)
        self.score_image = self.font.render(score_str, True, self.text_color)

        # 将得分放在屏幕右上角
        self.score_rect = self.score_image.get_rect()
        self.score_rect.right = self.screen_rect.right - 20
        self.score_rect.top = 20

    def show_score(self):
        """在屏幕上显示关卡和得分"""
        self.screen.blit(self.score_image, self.score_rect)
        self.screen.blit(self.highest_score_image, self.highest_score_rect)
        self.screen.blit(self.level_image, self.level_rect)

        # 绘制飞船和武器贴图
        self.spacecraft_mappings.draw(self.screen)
        self.bullet_mappings.draw(self.screen)

    def show_win_str(self):
        """在屏幕上显示获胜信息"""
        self.screen.blit(self.win_str_image, self.win_str_rect)

    def show_lost_str(self):
        """在屏幕上显示失败信息"""
        self.screen.blit(self.lost_str_image, self.lost_str_rect)

    def prep_highest_score(self):
        """将最高得分转换为渲染的图像"""
        highest_score = round(self.stats.highest_score, -1)
        highest_score_str = "{:,}".format(highest_score)
        self.highest_score_image = self.font.render(highest_score_str, True, self.text_color)

        # 将最高分放在屏幕顶部中央
        self.highest_score_rect = self.highest_score_image.get_rect()
        self.highest_score_rect.centerx = self.screen_rect.centerx
        self.highest_score_rect.top = self.score_rect.top

    def prep_level(self):
        """将等级转换为渲染的图像"""
        self.level_image = self.font.render(str(self.stats.level), True, self.text_color)

        # 将等级放在得分下方
        self.level_rect = self.level_image.get_rect()
        self.level_rect.right = self.screen_rect.right - 20
        self.level_rect.top = self.score_rect.bottom + 10

    def prep_spacecraft_mappings(self):
        """显示还余下多少艘飞船"""
        self.spacecraft_mappings = Group()
        for spacecraft_mapping_number in range(self.stats.spacecraft_life):
            spacecraft_mapping = Spacecraft_Mapping(self.screen)
            spacecraft_mapping.rect.x = 10 + spacecraft_mapping_number * spacecraft_mapping.rect.width\
                                        + spacecraft_mapping_number * 10
            spacecraft_mapping.rect.y = 10
            self.spacecraft_mappings.add(spacecraft_mapping)

    def prep_bullet_mapping(self):
        """准备子弹贴图显示"""
        self.bullet_mappings = Group()
        for bullet_mapping_number in range(self.settings.bullets_allowed):
            bullet_mapping = Bullet_Mapping(self.screen)
            bullet_mapping.rect.x = 5 + bullet_mapping_number * 15
            bullet_mapping.rect.y = 58
            self.bullet_mappings.add(bullet_mapping)

    def prep_win_str(self):
        """将胜利信息转换为一幅渲染图像"""
        win_str = self.stats.dictionaries['strs']['win_str']
        self.win_str_image = self.win_font.render(win_str, True, self.text_color)

        # 将胜利信息放置在屏幕中上部
        self.win_str_rect = self.win_str_image.get_rect()
        self.win_str_rect.centerx = self.screen_rect.centerx
        self.win_str_rect.centery = self.play_button.message_image_rect.centery - 150

    def prep_lost_str(self):
        """将失败信息转换为衣服渲染图像"""
        lost_str = self.stats.dictionaries['strs']['lost_str']
        self.lost_str_image = self.lost_font.render(lost_str, True, self.lost_str_color)

        # 将失败信息放在屏幕中上部
        self.lost_str_rect = self.lost_str_image.get_rect()
        self.lost_str_rect.centerx = self.screen_rect.centerx
        self.lost_str_rect.centery = self.play_button.message_image_rect.centery - 150

